package com.useeinfo.oa.modules.task.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @auther: bin.liu
 * @Date: 2019/7/5 15:06
 * @Description: 任务单工作日志表
 */
@Entity
@Table(name = "task_work_log")
public class TaskWorkLog extends SuperClassEntity<TaskWorkLog> {

    /**
     * 业务数据编号
     */
    private String code;

    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    @ManyToOne
    @JoinColumn(name = "task_order_id")
    private TaskOrder taskOrder;

    /**
     * 日志分类
     */
    @ManyToOne
    @JoinColumn(name = "work_log_cat")
    private DictInfo workLogCat;

    /**
     * 工作内容
     */
    private String workContent;

    /**
     * 重要程度
     */
    @ManyToOne
    @JoinColumn(name = "work_log_level")
    private DictInfo workLogLevel;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private SystemUser systemUser;

    /**
     * 日志照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "task_work_log_photo", joinColumns = {
            @JoinColumn(name = "log_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> logPhoto = new HashSet<Resource>();

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public TaskOrder getTaskOrder() {
        return taskOrder;
    }

    public void setTaskOrder(TaskOrder taskOrder) {
        this.taskOrder = taskOrder;
    }

    public DictInfo getWorkLogCat() {
        return workLogCat;
    }

    public void setWorkLogCat(DictInfo workLogCat) {
        this.workLogCat = workLogCat;
    }

    public String getWorkContent() {
        return workContent;
    }

    public void setWorkContent(String workContent) {
        this.workContent = workContent;
    }

    public DictInfo getWorkLogLevel() {
        return workLogLevel;
    }

    public void setWorkLogLevel(DictInfo workLogLevel) {
        this.workLogLevel = workLogLevel;
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    public Set<Resource> getLogPhoto() {
        return logPhoto;
    }

    public void setLogPhoto(Set<Resource> logPhoto) {
        this.logPhoto = logPhoto;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        if(getLogPhoto() != null){
            JSONArray jsonArray = new JSONArray();
            for(Resource resource : getLogPhoto()){
                jsonArray.add(resource.getPath());
            }
            jsonObject.put("logPhotos",jsonArray);
        }
        if (getLogPhoto() != null) {
            JSONArray jsonArray = new JSONArray();
            JSONObject json;
            for (Resource resource : getLogPhoto()) {
                json = new JSONObject();
                json.put("id",resource.getId());
                json.put("imgPath",StringConverters.null2String(resource.getPath()));
                jsonArray.add(json);
            }
            jsonObject.put("logPhotoArray", jsonArray);
        }

        jsonObject.put("workContent", StringConverters.null2String(getWorkContent()));
        jsonObject.put("workLogCatId",getWorkLogCat() == null ? "": StringConverters.null2String(getWorkLogCat().getId()));
        jsonObject.put("workLogCatName",getWorkLogCat() == null ? "":StringConverters.null2String(getWorkLogCat().getPropertyName()));
        jsonObject.put("code",StringConverters.null2String(getCode()));
        jsonObject.put("workLogLevelId",getWorkLogLevel() == null ? "" : StringConverters.null2String(getWorkLogLevel().getId()));
        jsonObject.put("workLogLevelName",getWorkLogLevel() == null ? "" : StringConverters.null2String(getWorkLogLevel().getPropertyName()));
        jsonObject.put("userName",StringConverters.null2String(getSystemUser().getUserName()));
        return jsonObject;
    }
}
